/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.dao;

import br.com.deployment.daogenerico.DaoGenerico;
import br.com.deployment.entidade.selo.TipoSelo;
import br.com.deployment.interfaces.TipoSeloDaoIn;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author elyjr
 */
@Stateless
@LocalBean
public class TipoSeloDao extends DaoGenerico<TipoSelo> implements TipoSeloDaoIn{

    public TipoSeloDao() {
        super(TipoSelo.class);
    }
    
    
    
    public TipoSelo findNome(String nome) {
        TipoSelo tipo = new TipoSelo();
        Query query = getEm().createQuery("select t from TipoSelo t where like :nome");
        query.setParameter("nome",tipo.getNome());
        tipo = (TipoSelo) query.getSingleResult();
        return tipo;  
    }
    
    public List<TipoSelo> findTipoEnd(TipoSelo tipoSelo) {
         List<TipoSelo> lista = new ArrayList<TipoSelo>();
        String sql = "select f from TipoSelo f ";  
        
        if(tipoSelo != null){
            if(tipoSelo.getNome() != null){
                sql += " where f.nome like :nome";
            }
        }
        Query query = getEm().createQuery(sql); 
        if(tipoSelo != null){
            if(tipoSelo.getNome() != null){
                query.setParameter("nome", "%"+tipoSelo.getNome()+"%");
            }
        }
               
        lista = query.getResultList();
        return lista;
    }
    
     public List<TipoSelo> findTipoSeloPesquisar(TipoSelo tipoSelo) {
           List<TipoSelo> listTipo = new ArrayList<TipoSelo>();
       
        if(tipoSelo != null){
           
             String sql = "Select t from TipoSelo t where ";
             
             if(tipoSelo.getNome()!= null && tipoSelo.getNome() != ""){
                 sql += "  lower(t.nome) like lower(?1) and"; 
             }
             if(tipoSelo.getDescricao()!= null && tipoSelo.getDescricao() != ""){
                 sql += "  lower(t.descricao) like lower(?2) and"; 
             }
              sql += " 1=1 ";
             Query query = getEm().createQuery(sql);
             if(tipoSelo.getNome()!= null && tipoSelo.getNome() != ""){
                 query.setParameter("1", "%" + tipoSelo.getNome() + "%");
             }
              if(tipoSelo.getDescricao()!= null && tipoSelo.getDescricao() != ""){
                 query.setParameter("2", "%" + tipoSelo.getDescricao() + "%");
             }
        
              listTipo = query.getResultList();
              System.out.println(sql);
        }
        
        return listTipo;
    }    
    
}
